﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace blackhouse.WebNemo.Business.Groups {
    public class ImpGroup : IGroup, IGroupOperations {

        #region Fields

        private IGroup[] allGroups;
        private bool readOnly;

        #endregion

        #region Properties

        /// <summary>
        /// Rozszerzenia, gdzie klucz odpowiada id grupy, a tablica wartości: 0 - recordExtension, 1 - groupExtension
        /// </summary>
        public Dictionary<int, string[]> Extensions { get; set; }

        public int Id { get; set; }
        public int ParentId { get; set; }
        public string Identyficator { get; set; }
        public string Name { get; set; }
        public string SystemName { get; set; }
        public bool Exists { get; set; }
        public GroupColumnCollection Columns { get; internal set; }
        public string SelectTable { get; set; }
        public KeyValuePair<int, string>[] GroupSystemNames { get; set; }
        public GroupType Type { get; set; }
        public bool EnableTranslate { get; set; }
        public bool ReadOnly {
            get {
                return this.Type != GroupType.Table || this.readOnly;
            }
            set {
                this.readOnly = value;
            }
        }

        #endregion

        #region Constructors

        public ImpGroup() {
            //this.Columns = new GroupColumnCollection();
        }

        #endregion

        #region Methods

        public void SetName(string name) {
            this.Name = name;
            this.SystemName = this.Name.RemovePolishChars().RemovePunctuation().CleanStringForUrl().ReplaceEx("-", "_").ToUpper();
        }

        public IGroup[] GetGroups() {
            return this.allGroups;
        }

        public void SetGroups(IGroup[] groups) {
            this.allGroups = groups;
        }

        public void Save() { }

        public void Delete() { }

        public bool CheckName(string checkName) {
            return true;
        }

        public IGroupRecord[] GetGroupRecords(CultureInfo language) {
            throw new NotImplementedException();
        }

        public IGroupRecord[] GetGroupRecords(GroupRecordQuery q, CultureInfo language) {
            throw new NotImplementedException();
        }

        public IGroupRecord GetGroupRecord(int recordId, CultureInfo language) {
            throw new NotImplementedException();
        }

        public IGroupRecord GetNewGroupRecord() {
            return new ImpGroupRecord();
        }

        public IGroupRecord[] GetRelatedGroupRecords(int groupId, string columnName, int recordId, CultureInfo language) {
            throw new NotImplementedException();
        }

        public void RemoveRelationToGroupRecord(int relRecordId, int parentGroupId, string parentColumnName, int parentRecordId) {
            throw new NotImplementedException();
        }

        public void AddRelationToGroupRecord(int relRecordId, int parentGroupId, string parentColumnName, int parentRecordId) {
            throw new NotImplementedException();
        }

        public string GetRecordExtensionName() {
            if (this.Extensions != null && this.Extensions.ContainsKey(this.Id))
                return this.Extensions[this.Id][0];
            else
                return string.Empty;
        }

        public string GetGroupExtensionName() {
            if (this.Extensions != null && this.Extensions.ContainsKey(this.Id))
                return this.Extensions[this.Id][1];
            else
                return string.Empty;
        }

        public int GetGroupId(string groupSystemName) {
            if (this.GroupSystemNames != null) {
                KeyValuePair<int, string> gsn = this.GroupSystemNames.FirstOrDefault(kvp => kvp.Value.Equals(groupSystemName, StringComparison.CurrentCultureIgnoreCase));
                return gsn.Key;
            }
            else
                return 0;
        }

        #endregion

    }
}
